
/**
 * 添加或者修改页面
 */
var StoreProcurementDetailedInfoDlg = {
    data: {
        orderOutNumber: "",
        goodsId: "",
        goodsName: "",
        goodsNumber: "",
        goodsPcode69: "",
        goodsSpecs: "",
        unit: "",
        rate: "",
        psNum: "",
        goodsWeight: "",
        outWeight: "",
        goodsVolume: "",
        outVolume: "",
        goodsPrice: "",
        outMoney: "",
        batch: "",
        detailedDate: "",
        notes: "",
        getUnitId: "",
        getUnit: "",
        v1: "",
        v2: "",
        v3: "",
        v4: ""
    }
};

var HxBasicGoodsInfoDlg = {
    data: {
        id:"",
        goodsCustomerId: "",
        goodsCustomerName: "",
        goodsName: "",
        goodsNumber: "",
        goodsPcode69: "",
        goodsXcode69: "",
        goodsHcode69: "",
        goodsHelpcode: "",
        goodsSpecs: "",
        dismantlingParts: "",
        goodsBigtype: "",
        goodsBrand: "",

        jibenUnit: "",
        kcUnit: "",
        kcRate: "",
        cgUnit: "",
        cgRate: "",
        pfUnit: "",
        pfRate: "",
        psUnit: "",
        psRate: "",

        anquanNum: "",
        goodsBottom: "",
        goodsLamination: "",
        goodsTraylayer: "",
        goodsSupportvolume: "",
        length: "",
        height: "",
        width: "",
        goodsWeight: "",
        goodsVolume: "",
        goodsChargingCg: "",
        goodsChargingPs: "",
        goodsChargingPf: "",
        goodsWarranty: ""
    }
};
layui.use(['form', 'admin', 'ax','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //选择生产日期后，填入批次
    laydate.render({
        elem:'#detailedDate'
    })

    let unitInfo = {};
    let goodsInfo = {};

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/storeProcurementDetailed/addItem", function (data) {
            if (data.success) {
                Feng.success("添加成功！");
                var Index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(Index);
            } else {
                Feng.error("添加失败！"+data.message);
            }
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    //点击选择商品名称
    $('#choiceGoods').click(function () {
        layer.open({
            type: 2,
            title: '选择商品',
            area: ['1000px', '450px'],
            content: Feng.ctxPath + '/hxBasicGoods/toChoiceGoods_od',
            end: function () {
                $("#goodsId").val(HxBasicGoodsInfoDlg.data.id);
                $("#goodsName").val(HxBasicGoodsInfoDlg.data.goodsName);//商品名称
                $("#goodsNumber").val(HxBasicGoodsInfoDlg.data.goodsNumber);
                $("#goodsPcode69").val(HxBasicGoodsInfoDlg.data.goodsPcode69);
                $("#goodsXcode69").val(HxBasicGoodsInfoDlg.data.goodsXcode69);
                $("#goodsHcode69").val(HxBasicGoodsInfoDlg.data.goodsHcode69);
                $("#goodsSpecs").val(HxBasicGoodsInfoDlg.data.goodsSpecs);
                $("#dismantlingParts").val(HxBasicGoodsInfoDlg.data.dismantlingParts);

                var goodsVolume = HxBasicGoodsInfoDlg.data.goodsVolume;
                var goodsWeight = HxBasicGoodsInfoDlg.data.goodsWeight;
                $("#unit").empty();
                goodsInfo = {};
                //配送单位
                let obj = {};
                obj['unit'] = HxBasicGoodsInfoDlg.data.psUnit;
                obj['rate'] = HxBasicGoodsInfoDlg.data.psRate;
                goodsInfo['psUnit']=obj;
                //基本单位
                obj = {};
                obj['unit'] = HxBasicGoodsInfoDlg.data.jibenUnit;
                obj['rate'] = 1;
                goodsInfo['jibenUnit']=obj;
                //库存单位
                obj = {};
                obj['unit'] = HxBasicGoodsInfoDlg.data.kcUnit;
                obj['rate'] = HxBasicGoodsInfoDlg.data.kcRate;
                goodsInfo['kcUnit']=obj;
                //采购单位
                obj = {};
                obj['unit'] = HxBasicGoodsInfoDlg.data.cgUnit;
                obj['rate'] = HxBasicGoodsInfoDlg.data.cgRate;
                goodsInfo['cgUnit']=obj;
                //批发单位
                obj = {};
                obj['unit'] = HxBasicGoodsInfoDlg.data.pfUnit;
                obj['rate'] = HxBasicGoodsInfoDlg.data.pfRate;
                goodsInfo['pfUnit']=obj;
                //商品其他信息
                obj = {};
                obj['goodsChargingPs'] = HxBasicGoodsInfoDlg.data.goodsChargingPs;
                obj['goodsVolume'] = goodsVolume;
                obj['goodsWeight'] = goodsWeight;
                goodsInfo['goods']=obj;

                unitInfo = {};
                //配送单位
                unitInfo[HxBasicGoodsInfoDlg.data.psUnit]=HxBasicGoodsInfoDlg.data.psRate;
                $("#unit").append(new Option( HxBasicGoodsInfoDlg.data.psUnit, HxBasicGoodsInfoDlg.data.psUnit));// 下拉菜单里添加元素
                //基本单位
                if(!unitInfo[HxBasicGoodsInfoDlg.data.jibenUnit]){
                    unitInfo[HxBasicGoodsInfoDlg.data.jibenUnit]=1;
                    $("#unit").append(new Option( HxBasicGoodsInfoDlg.data.jibenUnit, HxBasicGoodsInfoDlg.data.jibenUnit));// 下拉菜单里添加元素
                }
                //库存单位
                if(!unitInfo[HxBasicGoodsInfoDlg.data.kcUnit]){
                    unitInfo[HxBasicGoodsInfoDlg.data.kcUnit]=HxBasicGoodsInfoDlg.data.kcRate;
                    $("#unit").append(new Option( HxBasicGoodsInfoDlg.data.kcUnit, HxBasicGoodsInfoDlg.data.kcUnit));// 下拉菜单里添加元素
                }
                //采购单位
                if(!unitInfo[HxBasicGoodsInfoDlg.data.cgUnit]){
                    unitInfo[HxBasicGoodsInfoDlg.data.cgUnit]=HxBasicGoodsInfoDlg.data.cgRate;
                    $("#unit").append(new Option( HxBasicGoodsInfoDlg.data.cgUnit, HxBasicGoodsInfoDlg.data.cgUnit));// 下拉菜单里添加元素
                }
                //批发单位
                if(!unitInfo[HxBasicGoodsInfoDlg.data.pfUnit]){
                    unitInfo[HxBasicGoodsInfoDlg.data.pfUnit]=HxBasicGoodsInfoDlg.data.pfRate;
                    $("#unit").append(new Option( HxBasicGoodsInfoDlg.data.pfUnit, HxBasicGoodsInfoDlg.data.pfUnit));// 下拉菜单里添加元素
                }
                //配送单位
                if(!unitInfo[HxBasicGoodsInfoDlg.data.psUnit]){
                    unitInfo[HxBasicGoodsInfoDlg.data.psUnit]=HxBasicGoodsInfoDlg.data.psRate;
                    $("#unit").append(new Option( HxBasicGoodsInfoDlg.data.psUnit, HxBasicGoodsInfoDlg.data.psUnit));// 下拉菜单里添加元素
                }

                $("#unit").val(HxBasicGoodsInfoDlg.data.psUnit);
                $("#rate").val(HxBasicGoodsInfoDlg.data.psRate);
                form.render("select");

                if(parseFloat(HxBasicGoodsInfoDlg.data.psRate) == parseFloat(HxBasicGoodsInfoDlg.data.cgRate)){
                    $("#goodsVolume").val(goodsVolume);
                    $("#goodsWeight").val(goodsWeight);
                }
                if(parseFloat(HxBasicGoodsInfoDlg.data.psRate) < parseFloat(HxBasicGoodsInfoDlg.data.cgRate)){
                    //如果配送换算率 小于 采购换算率：  配送体积  =  采购体积/（采购换算率/配送换算率）
                    var div = accDiv(HxBasicGoodsInfoDlg.data.cgRate,HxBasicGoodsInfoDlg.data.psRate)
                    var divv_v = accDiv(goodsVolume,div).toFixed(4)
                    $("#goodsVolume").val(divv_v);
                    var divv_w = accDiv(goodsWeight,div).toFixed(3)
                    $("#goodsWeight").val(divv_w);
                }
                if(parseFloat(HxBasicGoodsInfoDlg.data.psRate) > parseFloat(HxBasicGoodsInfoDlg.data.cgRate)){
                    //如果配送换算率 大于 采购换算率：  配送体积  =  采购体积*（配送换算率/采购换算率）
                    var div = accDiv(HxBasicGoodsInfoDlg.data.psRate,HxBasicGoodsInfoDlg.data.cgRate)
                    var divm_v = accMul(goodsVolume,div).toFixed(4)
                    $("#goodsVolume").val(divm_v);
                    var divm_w = accMul(goodsWeight,div).toFixed(3)
                    $("#goodsWeight").val(divm_w);
                }
                $("#goodsPrice").val(accMul(HxBasicGoodsInfoDlg.data.psRate,HxBasicGoodsInfoDlg.data.goodsChargingPs).toFixed(3));
            }
        });
    });

    //除以的精确结果
    function accDiv(arg1,arg2){
        var t1=0,t2=0,r1,r2;
        try{
            t1=arg1.toString().split(".")[1].length;
        }catch(e){}

        try{
            t2=arg2.toString().split(".")[1].length;
        }catch(e){}

        with(Math){
            r1=Number(arg1.toString().replace(".",""));
            r2=Number(arg2.toString().replace(".",""));
            return (r1/r2)*pow(10,t2-t1);
        }
    };

    //乘以的精确结果
    function accMul(arg1,arg2){
        var m=0,s1=arg1.toString(),
            s2=arg2.toString();
        try{
            m+=s1.split(".")[1].length}catch(e){}
        try{
            m+=s2.split(".")[1].length}catch(e){}
        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m
        )};

    //输入数量
    $("#psNum").keyup(function(){
        var goodsName = $("#goodsName").val();
        if (goodsName==null||goodsName=='') {
            layer.msg('请选择商品');
        } else {
            var psNum = $("#psNum").val();//应入数量
            var goodsVolume = $("#goodsVolume").val();//体积
            var goodsWeight = $("#goodsWeight").val();//重量
            var goodsPrice = $("#goodsPrice").val();//单价
            $("#outMoney").val(accMul(psNum,goodsPrice).toFixed(3));//改变金额
            $("#outVolume").val(accMul(psNum,goodsVolume).toFixed(4));//改变应入体积
            $("#outWeight").val(accMul(psNum,goodsWeight).toFixed(3));//改变应入重量
        }
    });

    //输入单价
    $("#goodsPrice").keyup(function(){
        var goodsName = $("#goodsName").val();
        if (goodsName==null||goodsName=='') {
            layer.msg('请选择商品');
        } else {
            var psNum = $("#psNum").val();//应入数量
            var goodsPrice = $("#goodsPrice").val();//单价
            $("#outMoney").val(accMul(psNum,goodsPrice).toFixed(3));//改变金额
        }
    });

    //输入金额
    $("#outMoney").keyup(function(){
        var goodsName = $("#goodsName").val();
        if (goodsName==null||goodsName=='') {
            layer.msg('请选择商品');
        } else {
            var psNum = $("#psNum").val();//应入数量
            var outMoney = $("#outMoney").val();//金额
            $("#goodsPrice").val(accDiv(outMoney,psNum).toFixed(8));//改变单价
        }
    });

    //选择单位
    form.on('select(unit)', function(data){
        //获取选择的单位名称
        let unitVal = $("#unit").val();
        //根据单位名称获取对应的换算率
        let unitRate = unitInfo[unitVal];
        //设置对应的换算率
        $("#rate").val(unitRate);
        //获取当前货品的体积、重量和配送价格
        let goodsVolume = goodsInfo['goods'].goodsVolume;
        let goodsWeight = goodsInfo['goods'].goodsWeight;
        let goodsChargingPs = goodsInfo['goods'].goodsChargingPs;

        //转换体积和重量
        if(parseFloat(unitRate) == parseFloat(goodsInfo['cgUnit'].rate)){
            $("#goodsVolume").val(goodsVolume);
            $("#goodsWeight").val(goodsWeight);
        }
        if(parseFloat(unitRate) < parseFloat(goodsInfo['cgUnit'].rate)){
            //如果配送换算率 小于 采购换算率：  配送体积  =  采购体积/（采购换算率/配送换算率）
            var div = accDiv(goodsInfo['cgUnit'].rate,unitRate)
            var divv_v = accDiv(goodsVolume,div).toFixed(4)
            $("#goodsVolume").val(divv_v);
            var divv_w = accDiv(goodsWeight,div).toFixed(3)
            $("#goodsWeight").val(divv_w);
        }
        if(parseFloat(unitRate) > parseFloat(goodsInfo['cgUnit'].rate)){
            //如果配送换算率 大于 采购换算率：  配送体积  =  采购体积*（配送换算率/采购换算率）
            var div = accDiv(unitRate,goodsInfo['cgUnit'].rate)
            var divm_v = accMul(goodsVolume,div).toFixed(4)
            $("#goodsVolume").val(divm_v);
            var divm_w = accMul(goodsWeight,div).toFixed(3)
            $("#goodsWeight").val(divm_w);
        }
        //换算单价
        $("#goodsPrice").val(accMul(unitRate,goodsChargingPs).toFixed(3));
        let psNum = $("#psNum").val();//应入数量
        let goodsPrice = $("#goodsPrice").val();//单价
        //重新计算
        $("#outMoney").val(accMul(psNum,goodsPrice).toFixed(3));//改变金额
        $("#outVolume").val(accMul(psNum,goodsVolume).toFixed(4));//改变应入体积
        $("#outWeight").val(accMul(psNum,goodsWeight).toFixed(3));//改变应入重量
    });

    //返回按钮
    $("#backupPage").click(function () {
        var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});